package com.sxbang.friday.service.impl;

import com.sxbang.friday.base.result.Results;
import com.sxbang.friday.dao.RoleUserDao;
import com.sxbang.friday.dao.UserDao;
import com.sxbang.friday.dto.UserDto;
import com.sxbang.friday.model.SysRoleUser;
import com.sxbang.friday.model.SysUser;
import com.sxbang.friday.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleUserDao roleUserDao;
    @Override
    public SysUser getUser(String username) {
        return userDao.getUser(username);
    }

    @Override
    public int insertUser(SysUser sysUser) {
        return userDao.save(sysUser);
    }

    @Override//分页查询
    public Results<SysUser> selectUser(Integer offset, Integer limit) {
        //要返回两个参数：count 和 userlist
        return Results.success(userDao.countAllUsers().intValue(),userDao.selectUser(offset,limit));//返回两条数据，数据总条数，和分页后的数据
    }

    public Results save(SysUser sysUser, Integer roleId) {
//        return null;
        //存储user表，存储
        if(roleId != null){    //如果角色id存在，那么新增用户sysUser，并为该用户设置角色
        userDao.save(sysUser);
        SysRoleUser sysRoleUser = new SysRoleUser();
        sysRoleUser.setRoleId(roleId);
        sysRoleUser.setUserId(sysUser.getId().intValue());
        roleUserDao.save(sysRoleUser);
        return Results.success();
        }else{
            return Results.failure();
        }
    }

    @Override
    public SysUser getUserByPhone(String telephone) {
        return userDao.getUserByPhone(telephone);
    }

    @Override
    public SysUser getUserById(Long id) {
        return userDao.getUserById(id);
    }

    @Override
    public Results<SysUser> updateUser(UserDto userDto, Integer roleId) {
       if(roleId != null){
           //存在就更新，不存在就保存sysuser
           userDao.updateUser(userDto);
           //修改两张表 sysuser sysroleuser
           SysRoleUser sysRoleUser = new SysRoleUser();
           sysRoleUser.setUserId(userDto.getId().intValue());
           sysRoleUser.setRoleId(roleId);
           //先查
           if(roleUserDao.getSysRoleUserByUserId(userDto.getId().intValue()) != null){
               //做更新操作
               roleUserDao.updateSysRoleUser(sysRoleUser);
           }else{
               //保存
               roleUserDao.save(sysRoleUser);

           }
           return Results.success();
       }else{
           return Results.failure();
       }
    }

    @Override
    public int deleteUser(Long id) {
        //sysroleuser
        roleUserDao.deleteRoleUserByUserId(id.intValue());
        //sysuser
        return userDao.deleteUser(id.intValue());
    }

    @Override
    public Results<SysUser> getUserByFuzzyUsername(String username, Integer offset, Integer limit) {
        return Results.success(userDao.getUserByFuzzyUsername(username).intValue(),userDao.getUserByFuzzyUsernameByPage(username,offset,limit));
    }
}
